<?php

/**
 * 
 * ClientEngage: ClientEngage Visitor Voice (http://www.clientengage.com)
 * Copyright 2012, ClientEngage (http://www.clientengage.com)
 *
 * You must have purchased a valid license from CodeCanyon in order to have 
 * the permission to use this file.
 * 
 * You may only use this file according to the respective licensing terms 
 * you agreed to when purchasing this item on CodeCanyon.
 * 
 * 
 * 
 *
 * @author          ClientEngage <contact@clientengage.com>
 * @copyright       Copyright 2013, ClientEngage (http://www.clientengage.com)
 * @link            http://www.clientengage.com ClientEngage
 * @since           ClientEngage - Visitor Voice v 1.0
 * 
 */
App::uses('AppModel', 'Model');

/**
 * FieldOption Model
 *
 * @property Field $Field
 * @property User $User
 * @property FieldOptionResponse $FieldOptionResponse
 * @property Response $Response
 */
class FieldOption extends AppModel
{

    public $name = 'FieldOption';

    /**
     * Display field
     *
     * @var string
     */
    public $displayField = 'title';

    /**
     * Behaviours to be loaded
     * @var array
     */
    public $actsAs = array(
        'Utility.List' => array(
            'positionColumn' => 'position',
            'scope' => 'field_id')
    );

    public function __construct($id = false, $table = null, $ds = null)
    {
        parent::__construct($id, $table, $ds);

        if (Configure::read('demo') === true)
            $this->Behaviors->unload('Utility.List');
    }

    /**
     * Validation rules
     *
     * @var array
     */
    public $validate = array(
        'title' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                'message' => 'Please enter a title.',
            ),
            'maxlength' => array(
                'rule' => array('maxLength', 250),
                'message' => 'The title may not exceed 250 characters.'
            )
        ),
    );

    /**
     * belongsTo associations
     *
     * @var array
     */
    public $belongsTo = array(
        'Field' => array(
            'className' => 'Field',
            'foreignKey' => 'field_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

    /**
     * hasMany associations
     *
     * @var array
     */
    public $hasMany = array(
        'FieldOptionResponse' => array(
            'className' => 'FieldOptionResponse',
            'foreignKey' => 'field_option_id',
            'dependent' => true,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'Response' => array(
            'className' => 'Response',
            'foreignKey' => 'field_option_id',
            'dependent' => true,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );
    public $hasAndBelongsToMany = array(
        'Response' => array(
            'className' => 'Response',
            'joinTable' => 'field_option_responses',
            'foreignKey' => 'field_option_id',
            'unique' => 'keepExisting',
            'associationForeignKey' => 'response_id',
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'finderQuery' => '',
            'deleteQuery' => '',
            'insertQuery' => '',
            'with' => 'FieldOptionResponse'
        )
    );

    public function beforeSave($options = array())
    {
        return parent::beforeSave($options);
    }

    public function afterSave($created)
    {
        parent::afterSave($created);
    }

    public function beforeDelete($cascade = true)
    {
        return parent::beforeDelete($cascade);
    }

    public function afterDelete()
    {
        parent::afterDelete();
    }

}
